package com.dai.zhenfujin.my.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.UIUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 答题赚钱
 */
public class AnswerActivity extends BaseActivity implements View.OnClickListener{

    private TextView tvTitle,tv_period;
    private ImageView imageView;


    private Button btA,btB,btC,btD,btNotarize,btCancel;
    private String q_id;
    private String answer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);

        initView();
        initData();
        initListener();


    }

    private void initData() {
        Map<String, Object> params5 = new HashMap<>();
        Map<String, String> params6 = new HashMap<>();
        params5.put("req_bd", params6);
        HttpClient.post(AnswerActivity.this, Constants.QUERYQUESTION, params5, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean resultBean) {
                if (TextUtils.isEmpty(resultBean.q_id)){
                    tv_period.setVisibility(View.INVISIBLE);
                    btNotarize.setVisibility(View.INVISIBLE);
                    return;
                }
                q_id = resultBean.q_id;
                String  q_period = resultBean.q_period;
                tv_period.setText(q_period);
            }
        });
    }

    private void initListener() {
        imageView.setOnClickListener(this);
        btA.setOnClickListener(this);
        btB.setOnClickListener(this);
        btC.setOnClickListener(this);
        btD.setOnClickListener(this);
        btNotarize.setOnClickListener(this);
        btCancel.setOnClickListener(this);
    }

    private void initView() {
        tvTitle = findViewById(R.id.title_textview);
        tvTitle.setText("答题赚钱");
        imageView = findViewById(R.id.title_imageview);
        tv_period = findViewById(R.id.tv_period);

        btA = findViewById(R.id.answer_a);
        btB = findViewById(R.id.answer_b);
        btC = findViewById(R.id.answer_c);
        btD = findViewById(R.id.answer_d);
        btNotarize = findViewById(R.id.answer_notarize);
        btCancel = findViewById(R.id.answer_cancel);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_imageview://返回
                finish();
                break;
            case R.id.answer_a://按钮a
                answer = "a";
                btA.setBackgroundColor(getResources().getColor(R.color.yellow));
                btB.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btC.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btD.setBackgroundColor(getResources().getColor(R.color.gray_1));
                break;
            case R.id.answer_b://按钮b
                answer = "b";
                btB.setBackgroundColor(getResources().getColor(R.color.yellow));
                btA.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btC.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btD.setBackgroundColor(getResources().getColor(R.color.gray_1));
                break;
            case R.id.answer_c://按钮c
                answer = "c";
                btC.setBackgroundColor(getResources().getColor(R.color.yellow));
                btA.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btB.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btD.setBackgroundColor(getResources().getColor(R.color.gray_1));
                break;
            case R.id.answer_d://按钮d
                answer = "d";
                btD.setBackgroundColor(getResources().getColor(R.color.yellow));
                btA.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btB.setBackgroundColor(getResources().getColor(R.color.gray_1));
                btC.setBackgroundColor(getResources().getColor(R.color.gray_1));
                break;
            case R.id.answer_notarize://确认
                saveAnswer();
                break;
            case R.id.answer_cancel://取消
                break;
        }
    }

    private void saveAnswer() {
        Map<String, Object> params5 = new HashMap<>();
        Map<String, String> params6 = new HashMap<>();
        params6.put("q_id",q_id);
        params6.put("a_value",answer);
        params5.put("req_bd", params6);
        HttpClient.post(AnswerActivity.this, Constants.SAVEANSWER, params5, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean resultBean) {
                if (resultBean.state == 1){
                    UIUtils.showToastSafe(resultBean.msg);
                }else {
                    UIUtils.showToastSafe("回答错误");
                }
            }
        });
    }
}
